<?php
namespace Admin\Controller;
use Think\Controller;
class AdminUserController extends CommonController {
    
    public function index(){
        $list = M('administrator')->select();
        $this->assign('list',$list);
        $this->display();
    }
    public function add(){
        if(IS_POST){
            $data['username'] = I('username');
            $data['pwd'] = md5(I('pwd'));
            $data['last_login_time'] = date('Y-m-d H:i:s',time());
            //判断两次密码是否一致
            if(I('pwd') != I('repwd')){
                $this->error('两次密码不一致，请重新输入');
            }
            if(M('administrator')->add($data)){
                $this->success('添加成功',U('index'));
            }else{
                $this->error('添加失败');
            }
        }
        $this->display();
    }

    public function del(){
        $id = I('uid');
        $info = M('administrator')->where('id='.$id)->delete();
        if($info){
            $this->success('删除成功',U('index'));
        }else{
            $this->error('数据异常',U('index'));
        }
    }

    public function edit(){
        if(IS_POST){
            $id = I('id');
            $data['pwd'] = md5(I('pwd'));
            //判断两次密码是否一致
            if(I('pwd') != I('repwd')){
                $this->error('两次密码不一致，请重新输入');
            }
            if(M('administrator')->where('id='.$id)->save($data)){
                $this->success('修改成功',U('index'));
            }else{
                $this->error('修改失败');
            }
        }
        //查询用户详情
        $info = M('administrator')->where('id='.I('id'))->find();
        $this->assign('info',$info);
        $this->display();
    }
}